package com.xiyoufang.aij.circle.handler;

import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.xiyoufang.aij.circle.event.InviteEvent;
import com.xiyoufang.aij.circle.response.InviteEventListResponse;
import com.xiyoufang.aij.circle.response.InviteEventResponse;
import com.xiyoufang.aij.core.AiJCoreDb;
import com.xiyoufang.aij.core.AiJDbPro;
import com.xiyoufang.aij.core.AppConfig;
import com.xiyoufang.aij.core.ResponseFactory;
import com.xiyoufang.aij.event.Event;
import com.xiyoufang.aij.handler.EventHandler;
import com.xiyoufang.aij.utils.Json;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.websocket.common.WsResponse;

import java.util.ArrayList;
import java.util.List;

public class InviteHandler extends EventHandler<InviteEvent> {

    public InviteHandler() {
        super(InviteEvent.class);
    }

    @Override
    protected void handle(InviteEvent event, ChannelContext channelContext) {
        AiJDbPro uc = AiJCoreDb.uc();
        SqlPara sqlPara = AiJCoreDb.uc().getSqlPara("core.get_msg_by_user_id", "1245518744009052160");
        List<Record> recordLsst = AiJCoreDb.uc().find(sqlPara);

        List<InviteEventResponse> list = new ArrayList<>();
        InviteEventListResponse instan = new InviteEventListResponse();
        recordLsst.forEach(record -> {
                    if (record != null) {
                        InviteEventResponse inviteEventResponse = ResponseFactory.success(InviteEventResponse.class, "查询成功");
                        inviteEventResponse.setCircleId(record.getStr("circle_id"));
                        inviteEventResponse.setDescription(record.getStr("description"));
                        inviteEventResponse.setCircleName(record.getStr("circle_name"));
                        inviteEventResponse.setStatus(record.getStr("status"));
                        inviteEventResponse.setCreatedTime(record.getStr("created_time"));
                        list.add(inviteEventResponse);
                    }
                }

        );
        instan.setList(list);
        Tio.send(channelContext, WsResponse.fromText(Json.toJson(instan), AppConfig.use().getCharset()));
    }
}
